(add-to-list 'load-path "~/.emacs.d/my")
(add-to-list 'load-path "~/.emacs.d/set")

(if (not (file-exists-p "~/.emacs.d/elpa/"))
    (make-directory "~/.emacs.d/elpa/"))

(let ((default-directory "~/.emacs.d/elpa/"))
	(normal-top-level-add-subdirs-to-load-path))

(require 'package) ;; You might already have this line
;; http://mirrors.163.com/elpa/melpa/ gnu org 
;;(setq package-archives '(("gnu"   . "https://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
;;                         ("melpa" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")))

(setq package-archives '(("gnu"   . "https://mirrors.163.com/elpa/gnu/")
                          ("melpa" . "https://mirrors.163.com/elpa/melpa/")))


;; (setq package-archives '(("gnu" . "https://mirrors.ustc.edu.cn/elpa/gnu/")
;;                          ("melpa" . "https://mirrors.ustc.edu.cn/elpa/melpa/")
;;                          ("melpa-stable" . "https://mirrors.ustc.edu.cn/elpa/melpa-stable/")
;;                          ("org" . "https://mirrors.ustc.edu.cn/elpa/org/")))


(package-initialize)

(require 'cl)

;; not use packages  php-mode company-php  web-mode elpy ob-ipython :ruby feature-mode ob-ipython w3m helm-dash helm-projectile helm markdown-mode zenburn-theme yasnippet yaml-mode web-mode tabbar scss-mode sass-mode php-mode org nginx-mode magit jump jinja2-mode jedi highlight-symbol flymake-ruby flycheck feature-mode expand-region ediprolog cucumber-goto-step compan––––y-irony company-c-headers coffee-mode bookmark+ solarized-theme graphviz-dot-mode lispy slime slime-company
(defvar my/packages '(s f magit helm expand-region tabbar projectile helm-projectile zenburn-theme company
                        web-mode
                        nginx-mode) "默认安装的软件包")

(setq package-selected-packages my/packages)

(defun my/packages-installed-p ()
  "测试pkg是否已经安装"
  (loop for pkg in my/packages
	when (not (package-installed-p pkg)) do (return nil)
	finally (return t)))

(unless (my/packages-installed-p)
  (message "%s" "Refreshing package database...")
  (package-refresh-contents)
  (dolist (pkg my/packages)
    (when (not (package-installed-p pkg))
      (package-install pkg))))


;; 禁用Tab制表符
(setq-default indent-tabs-mode nil);
;; 一般情况tab==2个空格
(setq default-tab-width 2)
;; javascript tab 相当于2个空格
(setq js-indent-level 2)
;; (setq python-indent-offset 4)

;; (load-theme 'solarized-dark t) ;;html模板不高亮
;; (load-theme 'zenburn t) ;;zenburn
(load-theme 'dracula t) ;;zenburn 



;; 改变Emacs要你回答yes的行为,按y或空格键表示yes，n表示no。
;; (fset 'yes-or-no-p 'y-or-n-p)

;;(require 'cloj-config)
(require 'base-config)
;; (require 'org-config)
(require 'helm-config)
(require 'tabbar-config)
                                        ;(require 'smart-mode-line-config) ;
                                        ;(require 'mysql-config)
(require 'base-config)
(require 'dired-config)
;;(require 'php-config)
(require 'misc-config)
                                        ;(require 'objc-config)
(require 'recentf-config)
(require 'mode-line-config)
(require 'prj-config)
(require 'font-config)
(require 'run-config)
;;(require 'mac-config)
;; (require 'py-config)                    ;
;; (require 'slime-config)
;; (require 'sly-config)
;; (require 'company-tern)

                                        ;(load "folding" 'nomessage 'noerror)
                                        ;(folding-mode-add-find-file-hook)
                                        ;  (folding-add-to-marks-list 'php-mode                "//{"  "//}"  nil t)
;; (require 'fold)
;; (require 'fold-set)
;; (add-hook 'after-init-hook 'global-company-mode)
(require 'company)
(global-company-mode)
(global-set-key [backtab]
                (lambda ()
                  (interactive)
                  (previous-multiframe-window)))

                                        ;c-tab 切换窗口
(global-set-key [(control tab)]
                (lambda ()
                  (interactive)
                  (next-multiframe-window)))

(setq *ib* "scratch\\|mess\\|Completions\\|ibuffer\\|nrepl-server\\|nrepl-error\\|nrepl-connection\\|nrepl-events\\|nrepl-doc\\|nrepl-macroexpansion\\|nrepl-result\\|Warnings\\|Help\\|tramp/ssh\\|inferior-lisp")


(add-to-list 'same-window-buffer-names "*nrepl*")

                                        ;(iswitchb-mode 1)
                                        ;(setq iswitchb-buffer-ignore '("^ " "*Messages*" "*Compile-Log*" "*GNU Emacs*","nrepl-server","nrepl-connection","nrepl-events","*Completions*"))

;; cua模式,会与c-c冲突,使用原有的复制模式,c-w 复制 c-v/y 粘贴,a-w 剪切

;;复制c-c c-v c-x
(setq cua-rectangle-mark-key (kbd "C-S-<return>"))
(cua-mode t)
(setq cua-auto-tabify-rectangles nil) ;; Don't tabify after rectangle commands
(setq cua-keep-region-after-copy t) ;; Standard Windows behaviour


;; https://www.emacswiki.org/emacs/CopyAndPaste
;; (setq mark-even-if-nonactive nil)


(global-set-key (kbd "C-v")  'yank)
(global-set-key (kbd "C-w")  'kill-ring-save)
(global-set-key (kbd "M-w")  'kill-region)



(defun qiang-comment-dwim-line (&optional arg)
  "Replacement for the comment-dwim command.
If no region is selected and current line is not blank and we are not at the end of the line,
then comment current line.
Replaces default behaviour of comment-dwim, when it inserts comment at the end of the line."
  (interactive "*P")
  (comment-normalize-vars)
  (if (and (not (region-active-p)) (not (looking-at "[ \t]*$")))
      (comment-or-uncomment-region (line-beginning-position) (line-end-position))
    (comment-dwim arg)))
(global-set-key "\M-;" 'qiang-comment-dwim-line)
(global-set-key (kbd "C-;") 'qiang-comment-dwim-line)  



(require 'tramp)
(setq tramp-default-method "ssh")
;; (setq tramp-default-method "scp")
;(setq tramp-default-user "root"
					;      tramp-default-host "192.168.1.101")
;; 不使用vc,加快启动
(setq vc-ignore-dir-regexp
      (format "\\(%s\\)\\|\\(%s\\)"
	      vc-ignore-dir-regexp
	      tramp-file-name-regexp))


;(setq frame-title-format '(multiple-frames "%b" ("" invocation-name)))
;(require  'gentooish)
;(setq default-frame-alist
;'((top . 100) (left . 100)
;))



;; 备份策略,一直什么用,而且同时保存远程服务器时,有点慢.backup policies
(setq make-backup-files nil)
(setq version-control nil)
;; (setq kept-old-versions 2)
;; (setq kept-new-versions 2)
;; (setq delete-old-versions t)

;; (defvar backup-dir (expand-file-name "~/.emacs.d/backups/"))
;; (defvar autosave-dir (expand-file-name "~/.emacs.d/autosave/"))
;; (setq backup-directory-alist (list (cons ".*" backup-dir)))
;; (setq auto-save-list-file-prefix autosave-dir)
;; (setq auto-save-file-name-transforms `((".*" ,autosave-dir t)))

;; (add-to-list 'backup-directory-alist
;;              (cons "~/.emacs.d/backups/"))
;; (setq tramp-backup-directory-alist backup-directory-alist)


(add-to-list 'backup-directory-alist
             (cons tramp-file-name-regexp nil))

;(add-to-list 'default-frame-alist '(fullscreen . maximized))


;(require 'powerline)

(setq eshell-aliases-file "~/.emacs.d/set/eshell-alias.el")

;; https://www.emacswiki.org/emacs/Desktop
(setq desktop-dirname             "~/"
      desktop-base-file-name      ".emacs.desktop"
      desktop-base-lock-name      "lock"
      desktop-path                (list desktop-dirname)
      desktop-save                t
      desktop-files-not-to-save   "^/ssh:/\\|^/sudo:" ;; "^/(ssh)|(sudo):" ;不保存tramp /ssh:
      desktop-load-locked-desktop t
      desktop-save-mode nil
      desktop-save-mode-hook nil
      desktop-save-hook nil
      desktop-buffer-args-list nil
      desktop-save-buffer nil
      ;; desktop-globals-to-clear 
      )
(desktop-save-mode 1)
;; 不保存以下minor-mode
(dolist (mode '(magit-file-mode whitespace-mode global-whitespace-mode slime-autodoc-mode hs-minor-mode helm-mode projectile-mode company-mode lispy-mode slime-mode eldoc-mode indent-tabs-mode))
  (push (list mode . nil)
        desktop-minor-mode-table))
(add-to-list 'desktop-modes-not-to-save 'dired-mode)
(add-to-list 'desktop-modes-not-to-save 'Info-mode)
(add-to-list 'desktop-modes-not-to-save 'info-lookup-mode)
(add-to-list 'desktop-modes-not-to-save 'fundamental-mode)

;; (push '(whitespace-mode . nil)
;;       desktop-minor-mode-table)
;; (push '(global-whitespace-mode . nil)
;;       desktop-minor-mode-table)
;; (push '(slime-autodoc-mode . nil)
;;       desktop-minor-mode-table)


(global-set-key  (kbd "C-%") 'match-paren)
          

;; (setq mail-aliases t)


;(require 'smart-tab)
;(global-smart-tab-mode 1)

;(speedbar-add-supported-extension ".clj")
;(add-to-list 'speedbar-fetch-etags-parse-list
;    '("\\.clj" . speedbar-parse-c-or-c++tag))

;(load "~/.emacs.d/elpa/nxhtml/autostart")










 ;; '(bmkp-last-as-first-bookmark-file "~/.emacs.d/bookmarks")
;; (add-hook 'auto-save-hook 'bookmark-save)
(setq bmkp-bmenu-state-file (convert-standard-filename "~/.bmk-bmenu-state-emacs.el"))
(setq bookmark-version-control t)
;;每当设置书签的时候都保存书签文件，否则只在你退出 Emacs 时保存 
(setq bookmark-save-flag 1)



;; (require 'nginx-mode)
;; (add-to-list 'auto-mode-alist '("/nginx/" . nginx-mode))


;(require 'crontab-mode)
;(add-to-list 'auto-mode-alist '("crontab$" . crontab-mode))

(require 'expand-region)
(global-set-key (kbd "C-=") 'er/expand-region)

;(require 'session)
;(setq session-save-file "~/.session.emacs")
;(add-hook 'after-init-hook 'session-initialize)

(require 'magit)
;(setq magit-last-seen-setup-instructions "1.4.0")
(setq magit-auto-revert-mode nil)



;;(add-hook 'after-init-hook #'global-flycheck-mode)
(require 'cmd)

;; (setq python-indent-offset 4)
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(package-selected-packages
   '(dracula-theme s f magit helm expand-region tabbar projectile helm-projectile zenburn-theme company web-mode nginx-mode))
 '(tabbar-separator '(0.5)))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )
